/************************************************************************
*	Creates the Elixhauser comorbidity measure for the NSW Admitted		*
*		Patients Data Collection (HOIST).								*
*	Uses the coding of Quan et al, Medical Care 2005; 43: 1130-1139		*
*		translated to ICD-10-AM by Michael Falster, October 2009.		*
*	Creates indicator variables for each condition, and a count of 		*
*		the number of comorbidities.									*
*	Does not include the DRG screen to remove conditions related to		*
*		the principal diagnosis. This should be implemented separately	*
*		if required.													*
*																		*
*	Macro parameters													*
*		infile : the input data file, which should have ICD-10-AM		*
*					diagnosis codes in variables
*					diagnosis_codeP-diagnosis_code??
*		outfile: the output file										*
*		firsticd: the first diagnosis code to examine for comorbidities	*
*					Currently ignores the principal
*					diagnosis (diagnosis_codeP) *
*					Will need modification if want
*					to include this field.	*
*		lasticd: the last diagnosis code to examine for comorbidities.	*
*					NSW APDC currently allows up
*					to 50 additional diagnosis codes	*
*					Systematic differences in coding depth can affect	*
*					comorbidity measures at different facilities, so it	*
*					may be appropriate to limit the number of codes		*
*					examined.											*
************************************************************************/

%macro elix(infile=, outfile=, firsticd=1, lasticd=50);


data &outfile;
	set &infile;
	attrib elix 
		format=5. length=3 
		label='Elixhauser count';
	length elix1-elix31 3;
	* initialise elixhauser indicators to 0;
	array el {*} elix1-elix31;
	do i=1 to 31;
		el[i] = 0;
	end;

	* does not consider diagnosis_codeP;
	array diags {*}  diagnosis_code1-diagnosis_code&lasticd;
   	*set up variable, length $3, to store first THREE characters of diagcodes,
     AND SECOND VARIABLE, LENGTH $4 TO STORE FIRST 4 CHARACTERS OF DIAG CODES
     for more efficient processing;
        attrib diag3 length=$3;
        attrib diag4 length=$4;
 	*loop through array looking for relevant diag codes;
 	*EACH TYPE HAS ITS OWN FLAG, AS WE NEED TO COUNT THE NUMBER OF FLAGS;
	   	do i=&firsticd to &lasticd WHILE (diags{i} ne ' ');
	  		diag3=diags{i};
	        diag4=diags{I};
	*CONGESTIVE HEART FAILURE (GRP1);
		IF 		diag3 in ('I43','I50')
			 OR	diag4 in ('I099','I110','I130','I132','I255','I420', 
						  'I425','I426','I427','I428','I429','P290') 
			 THEN elix1 =1;
	* CARDIAC ARRHYTHMIAS (GRP2);
		ELSE IF diag3 in ('I47','I48','I49')
			 OR diag4 in ('I441','I442','I443','I456','I459','R000', 
						  'R001','R008','T821','Z450','Z950')
			 THEN elix2 =1;
	* VALVULAR DISEASE (GRP3);
		ELSE IF ('I05' <=: DIAG3 <=: 'I08')
			 OR ('I34' <=: DIAG3 <=: 'I39')
			 OR diag4 IN ('A520','I091','I098')
			 OR ('Q230' <=: diag4 <=: 'Q233')
			 OR ('Z952' <=: diag4 <=: 'Z954')
		 	 THEN elix3 =1;
	* PULMONARY CIRCULATION DISORDERS (GRP4);
		ELSE IF DIAG3 IN ('I26','I27')
			 OR diag4 IN ('I280','I288','I289')
			 THEN elix4 =1;
	* PERIPHERAL VASCULAR DISORDERS (GRP5);
		ELSE IF DIAG3 IN ('I70', 'I71')
			 OR diag4 IN ('I731','I738','I739','I771','I790','I792',
						  'K551','K558','K559','Z958','Z959')
			 THEN elix5 =1;
	* HYPERTENSION, UNCOMPLICATED (GRP6);
		ELSE IF DIAG3 IN ('I10')
			 THEN elix6 =1;
	 * HYPERTENSION, COMPLICATED (GRP7);
		ELSE IF DIAG3 IN ('I11','I12','I13','I15')
			 THEN elix7 =1;
	* PARALYSIS (GRP8);
		ELSE IF DIAG3 IN ('G81','G82')
			 OR diag4 IN ('G041', 'G114', 'G801', 'G802', 'G839')
			 OR ('G830' <=: diag4 <=: 'G834')
			 THEN elix8 =1;
	* OTHER NEUROLOGICAL DISORDERS (GRP9);
		ELSE IF ('G10' <=: DIAG3 <=: 'G13')
			 OR ('G20' <=: DIAG3 <=: 'G22')
			 OR ('G35' <=: DIAG3 <=: 'G37') 
			 OR DIAG3 IN ('G32','G40','G41','R56')
			 OR diag4 IN ('G254','G255','G312','G318','G319','G931',
						  'G934','R470')
			 THEN elix9 =1;
	* CHRONIC PULMONARY DISEASE (GRP10);
		ELSE IF ('J40' <=: DIAG3 <=: 'J47')
			 OR ('J60' <=: DIAG3 <=: 'J67')
			 OR diag4 IN ('I278','I279','J684','J701','J703')
			 THEN elix10 =1;
	* DIABETES, UNCOMPLICATED (GRP11);
		ELSE IF diag4 IN ('E100','E101','E109','E110','E111','E119',
						  'E120','E121','E129','E130','E131','E139',
						  'E140','E141','E149')
			 THEN elix11 =1; 
	* DIABETES, COMPLICATED (GRP12);
		ELSE IF ('E102' <=: diag4 <=: 'E108') 
			 OR ('E112' <=: diag4 <=: 'E118')
			 OR ('E122' <=: diag4 <=: 'E128')
			 OR ('E132' <=: diag4 <=: 'E138')
			 OR ('E142' <=: diag4 <=: 'E148')
			 THEN elix12 =1; 			 
	* HYPOTHYROIDISM (GRP13);
		ELSE IF ('E00' <=: DIAG3 <=: 'E03')
			 OR diag4 IN ('E890')
			 THEN elix13 =1;
	* RENAL FAILURE (GRP14);
		ELSE IF DIAG3 IN ('N18','N19','N25')
			 OR diag4 IN ('I120','I131','N250','Z940','Z992')
			 OR ('Z490' <=: diag4 <=: 'Z492')
			 THEN elix14 =1;
	* LIVER DISEASE (GRP15);
		ELSE IF DIAG3 IN ('B18','I85','K70')
			 OR ('K72' <=: DIAG3 <=: 'K74')
			 OR diag4 IN ('I864','I982','K711','K717','K760','Z944')
			 OR ('K713' <=: diag4 <=: 'K715')
			 OR ('K762' <=: diag4 <=: 'K769')
			 THEN elix15 =1;
	* PEPTIC ULCER DISEASE EXCLUDING BLEEDING (GRP16);
		ELSE IF diag4 IN ('K257','K259','K267','K269','K277','K279',
						  'K287','K289')
			 THEN elix16 =1;
	* AIDS/HIV (GRP17);
		ELSE IF DIAG3 IN ('B24')
			 OR ('B20' <=: DIAG3 <=: 'B22')
			 THEN elix17 =1;
	* LYMPHOMA (GRP18);
		ELSE IF DIAG3 IN ('C88','C96')
			 OR ('C81' <=: DIAG3 <=: 'C85')
			 OR diag4 IN ('C900','C902')
			 THEN elix18 =1;
	* METASTATIC CANCER (GRP19);
		ELSE IF ('C77' <=: DIAG3 <=: 'C80')
			 THEN elix19 =1;
	* SOLID TUMOR WITHOUT METASTASIS (GRP20);
		ELSE IF ('C00' <=: DIAG3 <=: 'C26') 
			 OR ('C30' <=: DIAG3 <=: 'C34')
 			 OR ('C37' <=: DIAG3 <=: 'C41')
 			 OR ('C45' <=: DIAG3 <=: 'C58')
			 OR ('C60' <=: DIAG3 <=: 'C76')
			 OR DIAG3 IN ('C43','C97')
			 THEN elix20 =1;
	* RHEUMATOID ARTHRITIS / COLLAGEN CASCULAR DISEASES (GRP21);
		ELSE IF DIAG3 IN ('M05','M06','M08','M30','M45')
			 OR ('M32' <=: DIAG3 <=: 'M35')
			 OR diag4 IN ('L940','L941','L943','M120','M123','M461',
						  'M468','M469')
			 OR ('M310' <=: diag4 <=: 'M313')
			 THEN elix21 =1;
	* COAGULOPATHY (GRP22);
		ELSE IF ('D65' <=: DIAG3 <=: 'D68')
			 OR diag4 IN ('D691')
			 OR ('D693' <=: diag4 <=: 'D696')
			 THEN elix22 =1;
	* OBESITY (GRP23);
		ELSE IF DIAG3 IN ('E66')
			 THEN elix23 =1;
	* WEIGHT LOSS (GRP24);
		ELSE IF DIAG3 IN ('R64')
			 OR ('E40' <=: DIAG3 <=: 'E46')
			 OR diag4 IN ('R634')
			 THEN elix24 =1;
	* FLUID AND ELECTROLYTE DISORDERS (GRP25);
		ELSE IF DIAG3 IN ('E86','E87')
			 OR diag4 IN ('E222')
			 THEN elix25 =1;
	* BLOOD LOSS ANEMIA (GRP26);
		ELSE IF diag4 IN ('D500')
			 THEN elix26 =1;
	* DEFICIENCY ANEMIA (GRP27);
		ELSE IF ('D51' <=: DIAG3 <=: 'D53')
			 OR diag4 IN ('D508','D509')
			 THEN elix27 =1;
	* ALCOHOL ABUSE (GRP28);
		ELSE IF DIAG3 IN ('F10','E52','T51')
			 OR diag4 IN ('G621','I426','K292','K700','K703','K709',
						  'Z502','Z714','Z721')
			 THEN elix28 =1;
	* DRUG ABUSE (GRP29);
		ELSE IF DIAG3 IN ('F18','F19')
			 OR ('F11' <=: DIAG3 <=: 'F16')
			 OR diag4 IN ('Z715','Z722')
			 THEN elix29 =1;
	* PSYCHOSES (GRP30);
		ELSE IF DIAG3 IN ('F20','F28','F29')
			 OR ('F22' <=: DIAG3 <=: 'F25')
			 OR diag4 IN ('F302','F312','F315')
			 THEN elix30 =1;
	* DEPRESSION (GRP31);
		ELSE IF DIAG3 IN ('F32','F33')
			 OR diag4 IN ('F204','F341','F412','F432')
			 OR ('F313' <=: diag4 <='F315')
			 THEN elix31 =1;
	end;
	* hierarchy;
	if elix12 eq 1 then elix11 = 0;
	if elix19 eq 1 then elix20 = 0;

	* cleanup;
	drop i diag3 diag4;

	* count of comorbidities;
	elix = sum(of elix1-elix31);

run;
%mend elix;

